package org.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.demo.constant.HintConstant;
import org.example.demo.constant.InspectionStatusConstant;
import org.example.demo.entity.InformationHint;
import org.example.demo.entity.InspectionLog;
import org.example.demo.service.InformationHintService;
import org.example.demo.service.InspectionLogService;
import org.example.demo.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 考察记录
 */
@RestController
@RequestMapping("inspectionLog")
public class InspectionLogController {

    @Resource
    private InspectionLogService inspectionLogService;

    @Autowired
    private InformationHintService informationHintService;
    /**
     * 分页查询考察记录
     * 
     * @param inspectionLog 查询条件
     * @param pageNum       页码
     * @param pageSize      每页大小
     * @return 查询结果
     */
    @PostMapping("page")
    public AjaxResult page(@RequestBody InspectionLog inspectionLog,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<InspectionLog> page = inspectionLogService.selectPage(inspectionLog, pageNum, pageSize);

        Map<String, Object> result = new HashMap<>();
        result.put("page", page);
        result.put("total", page.getTotal());

        return AjaxResult.success(result);
    }

    /**
     * 根据ID查询考察记录
     * 
     * @param id 考察记录ID
     * @return 查询结果
     */
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable Long id) {
        InspectionLog inspectionLog = inspectionLogService.getById(id);

        if (inspectionLog != null) {
            return AjaxResult.success(inspectionLog);
        } else {
            return AjaxResult.error("考察记录不存在");
        }
    }

    /**
     * 新增考察记录
     * 
     * @param inspectionLog 考察记录信息
     * @return 操作结果
     */
    @PostMapping
    public AjaxResult add(@RequestBody InspectionLog inspectionLog) {
        if (inspectionLogService.save(inspectionLog)) {

            // 添加考察信息给信息提示实体
            InformationHint informationHint = InformationHint.builder()
                .userId(inspectionLog.getUserId())
                .title(HintConstant.INSPECTION_HINT)
                .content(InspectionStatusConstant.status[inspectionLog.getStatus()])
                .time(inspectionLog.getRecordDate())
                .build();

            informationHintService.save(informationHint);

            return AjaxResult.success();
        } else {
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 修改考察记录
     * 
     * @param inspectionLog 考察记录信息
     * @return 操作结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody InspectionLog inspectionLog) {
        if (inspectionLogService.updateById(inspectionLog)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除考察记录
     * 
     * @param id 考察记录ID
     * @return 操作结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Long id) {
        if (inspectionLogService.removeById(id)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("删除失败");
        }
    }
}